<template>
  <div class="page-search">
    <hSearch v-bind="props.searchformConfig" v-model="formData">
      <template #footer>
        <div class="search-btn">
          <el-button
            type="default"
            :icon="RefreshLeft"
            @click="handleResetClick"
            >重置</el-button
          >
          <el-button type="primary" :icon="Search" @click="handleQueryClick"
            >搜索</el-button
          >
        </div>
      </template>
    </hSearch>
  </div>
</template>

<script setup lang="ts">
import hSearch from '../../../base-ui/form';
import { Search, RefreshLeft } from '@element-plus/icons-vue';
import { ref, defineProps, defineExpose, defineEmits } from 'vue';
const emits = defineEmits(['handleQueryClick', 'handleResetClick']);
const props = defineProps({
  searchformConfig: {
    type: Object,
    required: true
  }
});
const formItems = props.searchformConfig.formItems ?? [];
const formOriginData: any = {};
for (const item of formItems) {
  formOriginData[item.field] = '';
}
const formData = ref(formOriginData);
const handleResetClick = () => {
  // 直接赋值导致form 组件 formData=ref({...props.modelValue}) 浅拷贝 没有依赖
  // 解决办法一: 循环formData.value[`${key}`] = formOriginData[key];赋值form.value的属性,触发 hForm组件ref 更新

  // 解决办法二:不是用 使用formData=ref({...props.modelValue})  以及 不使用循环,以及hForm对值不使用双向绑定:从而使用:model-value="modleValue[`${item.field}`]" 和 @update:modelValue='handleValueChange($event,item.field)'
  // handleValueChange(value:any,field:string){ emists('upadate:modelValue',{...props.modelValue,[field]:value}) }
  //
  for (const key in formOriginData) {
    // 解决办法一
    formData.value[`${key}`] = formOriginData[key];
    // 解决办法二
    // formData.value = formOriginData;
  }
  emits('handleResetClick');
};
const handleQueryClick = () => {
  console.log(111);
  emits('handleQueryClick', formData.value);
};

// 暴露方法
defineExpose({
  handleQueryClick,
  handleResetClick
});
</script>

<style scoped lang="less">
.search-btn {
  text-align: right;
  padding: 5px;
}
</style>
